package com.gtenacg.cartoonservice.controller;

import com.gtenacg.cartoonservice.common.annotation.CheckToken;
import com.gtenacg.cartoonservice.common.Result;
import com.gtenacg.cartoonservice.pojo.model.Admin;
import com.gtenacg.cartoonservice.service.AdminService;
import com.gtenacg.cartoonservice.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {

    final AdminService adminService;
    final JwtUtil jwtUtil;

    @PostMapping("/login")
    public Result<String> login(@RequestBody Admin admin) {
        log.info("管理员登录: {}", admin);
        admin = adminService.login(admin);
        if (admin == null) return new Result<>(HttpStatus.UNAUTHORIZED.value(), "Login_Failed", null);
        log.info("管理员登录成功: {}", admin);
        String token = jwtUtil.getToken(Map.of("id", admin.getId()));
        return Result.success(token);
    }
    @PostMapping("/token")
    public Result<Admin> autoLogin(@RequestBody String token) {
        log.info("自动登录 token: {}", token);
        Admin admin = null;
        try {
            Integer id = (Integer) jwtUtil.parseToken(token).get("id");
            admin = adminService.getById(id);
        } catch (JwtException e) {
            log.info("token 无效: {}", e.getMessage());
            return new Result<>(HttpStatus.UNAUTHORIZED.value(), "Token_Invalid", null);
        }
        return Result.success(admin);
    }

    @GetMapping("/token")
    public Result<Admin> getAdminByToken(String token) {
        log.info("获取管理员信息: {}", token);
        Claims claims = jwtUtil.parseToken(token);
        Integer id = (Integer) claims.get("id");
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }

    @CheckToken
    @GetMapping
    public Result<List<Admin>> list() {
        log.info("获取管理员列表");
        List<Admin> list = adminService.list();
        return Result.success(list);
    }

    @CheckToken
    @PostMapping
    public Result<Integer> insert(@RequestBody Admin admin) {
        log.info("保存管理员: {}", admin);
        Integer id = null;
        try {
            id = adminService.insert(admin);
        } catch (DuplicateKeyException e) {
            return new Result<>(HttpStatus.CONFLICT.value(), "Duplicate_Key", null);
        }
        return Result.success(id);
    }

    @CheckToken
    @PutMapping
    public Result<Void> update(@RequestBody Admin admin) {
        log.info("更新管理员: {}", admin);
        adminService.update(admin);
        return Result.success();
    }

    @CheckToken
    @DeleteMapping("/{ids}")
    public Result<Void> delete(@PathVariable List<Integer> ids) {
        log.info("删除管理员: {}", ids);
        adminService.delete(ids);
        return Result.success();
    }

    @CheckToken
    @GetMapping("/{id}")
    public Result<Admin> getById(@PathVariable Integer id) {
        log.info("获取管理员信息: {}", id);
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }

    @CheckToken
    @PutMapping("/{id}")
    public Result<Void> resetPassword(@PathVariable Integer id) {
        log.info("重置管理员密码: {}", id);
        adminService.resetPassword(adminService.getById(id));
        return Result.success();
    }






}
